/**
 * 
 */
package com.youth.fund.hk.hostManager.shiro.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.youth.fund.hk.hostManager.common.dao.ICommonDao;
import com.youth.fund.hk.hostManager.common.service.impl.CommonService;
import com.youth.fund.hk.hostManager.shiro.dao.UserDao;
import com.youth.fund.hk.hostManager.shiro.datamodel.User;
import com.youth.fund.hk.hostManager.shiro.service.PasswordHelper;
import com.youth.fund.hk.hostManager.shiro.service.RoleService;
import com.youth.fund.hk.hostManager.shiro.service.UserService;

/**
 * @author Sunk_Sky
 *
 * 2014年11月13日下午6:48:34
 */
@Service("UserService")
public class UserServiceImpl extends CommonService<User> implements UserService
{
	private UserDao userDao;
	
	@Autowired
	@Qualifier("PasswordHelper")
	private PasswordHelper passwordHelper;
	
	@Autowired
	@Qualifier("RoleService")
	private RoleService roleService;
	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.service.impl.CommonService#setCommonDao(com.youth.fund.hk.hostManager.common.dao.ICommonDao)
	 */
	@Autowired
	@Qualifier("UserDao")
	@Override
	public void setCommonDao(ICommonDao<User> commonDao)
	{
		this.commonDao = commonDao;
		
		this.userDao = (UserDao) commonDao;

	}
	 /**
     * 创建用户
     * @param user
     */
    public User createUser(User user) {
        //加密密码
        passwordHelper.encryptPassword(user);
        return userDao.createUser(user);
    }

    
    public User updateUser(User user) {
        return userDao.updateUser(user);
    }

    
    public void deleteUser(Long userId) {
        userDao.deleteUser(userId);
    }

    /**
     * 修改密码
     * @param userId
     * @param newPassword
     */
    public void changePassword(Long userId, String newPassword) {
        User user =userDao.findOne(userId);
        user.setPassword(newPassword);
        passwordHelper.encryptPassword(user);
        userDao.updateUser(user);
    }

    
    public User findOne(Long userId) {
        return userDao.findOne(userId);
    }

    
    public List<User> findAll() {
        return userDao.findAll();
    }

    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    /**
     * 根据用户名查找其角色
     * @param username
     * @return
     */
    public Set<String> findRoles(String username) {
        User user =findByUsername(username);
        if(user == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findRoles(user.getRoleIds().toArray(new Long[0]));
    }

    /**
     * 根据用户名查找其权限
     * @param username
     * @return
     */
    public Set<String> findPermissions(String username) {
        User user =findByUsername(username);
        if(user == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findPermissions(user.getRoleIds().toArray(new Long[0]));
    }
	/* (non-Javadoc)
	 * @see com.youth.fund.hk.hostManager.common.service.impl.CommonService#generateHtml(java.io.Serializable)
	 */
	@Override
	public void generateHtml(User model)
	{
		// TODO Auto-generated method stub
		
	}


}
